package com.css.bjdt_core.ryxx.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.ryxx.entity.DnDj;
import com.css.bjdt_core.ryxx.entity.DyInfo;
import com.css.bjdt_core.util.CommonUtil;

@Service
public class DyInfoService {

	@Autowired
	private Dao dao;

	public Map queryRyxxsPage(int pageNum, int pageSize, String idLst) {
		Map<String, Object> result = new HashMap<String, Object>();
		String[] arrids = {};
		if (StringUtils.isNoneBlank(idLst)) {
			arrids = idLst.split(",");
		} else {
			return result;
		}
		pageNum = (pageNum < 1) ? 1 : pageNum;
		pageSize = (pageSize < 1) ? 10 : pageSize;
		int start = (pageNum - 1) * pageSize;
		String[] tempids = Arrays.copyOfRange(arrids, start, start + pageSize);

		String sqlStr = "	select uuid,tmz.dzzjc,t.realname,t.birthday,xb.dmcpt as xbDmcpt,mz.dmcpt as mzDmcpt,t.usertype,tdzr.dmcpt as tdzrDmcpt,tdzrx.dmcpt as tdzrxDmcpt from DY_INFO t  "
				+ " left join T_M_ZZINFO tmz on t.partyorganid = tmz.dzzid "
				+ " left join t_dm_gb22611_xb xb on t.sexid = xb.dmcod "
				+ " left join t_dm_gb3304_mz mz on t.national = mz.dmcod "
				+ " left join T_DM_ZB64_RDSXL tdzr on t.edulevel = tdzr.dmcod "
				+ " left join T_DM_ZB86_RDSGZYX tdzrx on t.yxqk = tdzrx.dmcod " + " WHERE t.uuid in (@ryids)";
		//+ " AND T2.szzb = @dzzId";
		Sql sql = Sqls.create(sqlStr);

		//sql.params().set("dzzId", dzzId);
		sql.params().set("ryids", tempids);
		//Pager pager = dao.createPager(pageNum, pageSize);
		//sql.setPager(pager);
		sql.setCallback(new SqlCallback() {

			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<DyInfo> dyInfoList = new ArrayList<DyInfo>();
				while (rs.next()) {
					DyInfo dyInfo = new DyInfo();
					dyInfo.setUuid(rs.getString("uuid"));
					dyInfo.setSzzb(rs.getString("dzzjc"));
					dyInfo.setXingming(rs.getString("realname"));
					try {
						dyInfo.setChushengriqi(CommonUtil.getAge(rs.getString("birthday")));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						dyInfo.setChushengriqi("--");
					}
					dyInfo.setXingbie(rs.getString("xbDmcpt"));
					dyInfo.setMinzu(rs.getString("mzDmcpt"));
					String userTypeStr = rs.getString("usertype");
					switch (userTypeStr) {
					case "1":
						dyInfo.setRudangcsd("申请人");
						break;
					case "2":
						dyInfo.setRudangcsd("积极分子");
						break;
					case "3":
						dyInfo.setRudangcsd("拟发展对象");
						break;
					case "4":
						dyInfo.setRudangcsd("预备党员");
						break;
					case "5":
						dyInfo.setRudangcsd("党员");
						break;
					default:
						dyInfo.setRudangcsd("0000000");
						break;
					}
					dyInfo.setZuigaoxl(rs.getString("tdzrDmcpt"));
					dyInfo.setYixianqk(rs.getString("tdzrxDmcpt"));
					dyInfoList.add(dyInfo);
				}
				return dyInfoList;
			}
		});

		dao.execute(sql);
		List<DyInfo> lst = sql.getList(DyInfo.class);

		result.put("Rows", lst);
		/*Map<String, Object> params = new HashMap();
		params.put("ryids", arrids);*/
		int count = arrids.length; /*PageQuerys.countDistinctSql(dao, sqlStr, null, params);*/

		result.put("Total", count);
		return result;
	}

	public DyInfo getDyInfo(String uuid) {
		String sqlStr = "select distinct tmz.dzzjc,t.uuid,t.partyorganid,t.realname,t.identitycard,t.sexid,t.birthday,"
				+ " t.national,t.usertype,t.job,t.edulevel,t.yxqk,t.xshjc,t.applypartydate,t.jjfzbegindate,t.nfzdxbegindate"
				+ " from DY_INFO t left join T_M_ZZINFO tmz on t.partyorganid = tmz.dzzid where uuid = @uuid";
		Sql sql = Sqls.create(sqlStr);
		sql.params().set("uuid", uuid);
		sql.setCallback(new SqlCallback() {

			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<DyInfo> mRyInfoList = new ArrayList<DyInfo>();
				while (rs.next()) {
					DyInfo dyInfo = new DyInfo();
					dyInfo.setSzzb(rs.getString("dzzjc"));
					dyInfo.setUuid(rs.getString("uuid"));
					dyInfo.setPartyOrganid(rs.getString("partyorganid"));
					dyInfo.setRealName(rs.getString("realname"));
					dyInfo.setIdentitycard(rs.getString("identitycard"));
					dyInfo.setSexId(rs.getString("sexid"));
					dyInfo.setBirthday(rs.getDate("birthday"));
					dyInfo.setNational(rs.getString("national"));
					dyInfo.setUserType(rs.getString("usertype"));
					dyInfo.setJob(rs.getString("job"));
					dyInfo.setEduLevel(rs.getString("edulevel"));
					dyInfo.setYxqk(rs.getString("yxqk"));
					dyInfo.setXshjc(rs.getInt("xshjc"));
					dyInfo.setApplyPartyDate(rs.getDate("applypartydate"));
					dyInfo.setJjfzBeginDate(rs.getDate("jjfzbegindate"));
					dyInfo.setNfzdxBeginDate(rs.getDate("nfzdxbegindate"));
					mRyInfoList.add(dyInfo);
				}
				// TODO Auto-generated method stub
				return mRyInfoList;
			}

		});
		dao.execute(sql);
		List<DyInfo> lst = sql.getList(DyInfo.class);
		DyInfo obj = lst.get(0);
		return obj;
	}

	public void updateADyInfoByUuid(DyInfo dyInfo) {
		String sqlStr = "update DY_INFO SET REALNAME = @realName,SEXID = @sexId,BIRTHDAY = to_date(@birthday,'yyyy-mm-dd'),NATIONAL = @national,"
				+ "USERTYPE = @usertype,JOB = @job,EDULEVEL = @eduLevel,YXQK = @yxqk,XSHJC = @xshjc where UUID = @uuid";
		Sql sql = Sqls.create(sqlStr);
		sql.params().set("realName", dyInfo.getRealName()).set("sexId", dyInfo.getSexId())
				.set("birthday", dyInfo.getChushengriqi()).set("national", dyInfo.getNational())
				.set("usertype", dyInfo.getUserType()).set("job", dyInfo.getJob()).set("eduLevel", dyInfo.getEduLevel())
				.set("yxqk", dyInfo.getYxqk()).set("xshjc", dyInfo.getXshjc()).set("uuid", dyInfo.getUuid());
		dao.execute(sql);
	}

	public String getADnDjInfo(String dyId) {
		DnDj dnDj = dao.fetch(DnDj.class, Cnd.where("dy_id", "=", dyId));
		if (null == dnDj) {
			return "";
		} else {
			return dnDj.getRdszzb();
		}

	}
}
